package de.osflex.touarek.containers
{
	import flash.display.NativeWindowDisplayState;
	import flash.events.MouseEvent;
 
	import mx.containers.ApplicationControlBar;
	import mx.controls.Button;
	import mx.core.Application;
	
	/**
	 * @class SuperApplicationControlBar
	 * @author Andre Mörke	  
	 * @created 20081108
	 * 
	 * Extends the normal ApplicationControlBar so that it contains 
	 * come ApplicationControls for closing or minimizing the app
	 */
 
	public class SuperApplicationControlBar extends ApplicationControlBar
	{
		[Bindable] public var showControls:Boolean = false;
		[Bindable] public var trayButtonVisible:Boolean = true;
		[Bindable] public var controlsRightDistance:uint = 0;
		private var closeButton:Button = new Button();
		private var minimizeButton:Button = new Button();
		private var maximizeButton:Button = new Button();
 
		public function SuperApplicationControlBar()
		{
			super();								
		}
		
		/**
		 * Overrides the existing createChildren method
		 * for setting up the application controls
		 * 
		 */
		  
		override protected function createChildren():void
		{
			super.createChildren();						
			this.setStyle("highlightAlphas", [0.5, 0.10]);			
			this.setStyle("fillAlphas", [.5, .65]);
 
			if(this.showControls){
				this.closeButton.width = 10;
				this.closeButton.height = 10;
				this.closeButton.styleName = 'closeButton';
				this.closeButton.toolTip = 'Schliessen';			
				this.rawChildren.addChild(this.closeButton);
 
				this.minimizeButton.width = 10;
				this.minimizeButton.height = 10;
				this.minimizeButton.styleName = "trayButton";
				this.minimizeButton.toolTip = 'Minimieren';
				this.rawChildren.addChild(this.minimizeButton);
 
 				if(trayButtonVisible){
					this.maximizeButton.width = 10;
					this.maximizeButton.height = 10;
					this.maximizeButton.styleName = "increaseButton";
					this.maximizeButton.toolTip = "Maximieren";
					this.rawChildren.addChild(this.maximizeButton);
				}
			}
			this.posChildren();
			this.addListeners();
 
		}
		
		/**
		 * Adds all necessary listeners to the applicationcontrols
		 */
 
		protected function addListeners():void
		{
			this.closeButton.addEventListener(MouseEvent.CLICK, closeClickedEvent);
			this.minimizeButton.addEventListener(MouseEvent.CLICK, minimizeApplication);
			this.maximizeButton.addEventListener(MouseEvent.CLICK, maxMinAppliation);
			this.addEventListener(MouseEvent.MOUSE_DOWN, moveApplication);		
			this.doubleClickEnabled = true;
			this.addEventListener(MouseEvent.DOUBLE_CLICK, maxMinAppliation);
		}
		
		/**
		 * Maximizelistener
		 * 
		 * @param event MouseEvent
		 */
 
		protected function maxMinAppliation(event:MouseEvent):void
		{
			if(Application.application.nativeWindow.displayState == NativeWindowDisplayState.MAXIMIZED){
				Application.application.nativeWindow.restore();
				this.maximizeButton.toolTip = "Maximieren";
				this.maximizeButton.styleName = "increaseButton";
			}else{
				Application.application.maximize();
				this.maximizeButton.toolTip = "Verkleinern";
				this.maximizeButton.styleName = "decreaseButton";
			}
		}
		
		/**
		 * Movelistener
		 * 
		 * @param event MouseEvent
		 */
 
		protected function moveApplication(event:MouseEvent):void
		{
			Application.application.nativeWindow.startMove();
		}
 
 		/**
 		 * Minimizelistener
 		 * 
 		 * @param event MouseEvent
 		 */
 
		protected function minimizeApplication(event:MouseEvent):void{
			Application.application.minimize();
		}
		
		/**
		 * Closelistener
		 * 
		 * @param event MouseEvent
		 */
 
		protected function closeClickedEvent(event:MouseEvent):void
		{
			Application.application.exit();
		}
		
		/**
		 * Overriding the existing updateDisplayList method to reposition the controls
		 * 
		 * @param unscaledWidth Number
		 * @param unscaledHeight Number
		 */
 
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			this.posChildren();
		}
		
		/**
		 * Reorder the elements/controls
		 */
 
		protected function posChildren():void
		{
			this.closeButton.buttonMode = true;
			this.closeButton.useHandCursor = true;
			this.closeButton.x = Application.application.nativeWindow.width - this.closeButton.width - (20 + controlsRightDistance);
			this.closeButton.y = this.height / 2 - 5;
 
			this.minimizeButton.buttonMode = true;
			this.minimizeButton.useHandCursor = true;
			this.minimizeButton.x = Application.application.nativeWindow.width - this.minimizeButton.width - ((trayButtonVisible ? 60 : 40) + controlsRightDistance);
			this.minimizeButton.y = this.height / 2 - 5;					
 
 			if(trayButtonVisible){
				this.maximizeButton.buttonMode = true;
				this.maximizeButton.useHandCursor = true;
				this.maximizeButton.x = Application.application.nativeWindow.width - this.maximizeButton.width - (40 + controlsRightDistance);
				this.maximizeButton.y = this.height / 2 - 5;
			}
		}
	}

}